/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.music;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.utils.SlimComparator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.musicobjects.SlimAlbum;
import org.bff.slimserver.musicobjects.SlimArtist;
import org.bff.slimserver.musicobjects.SlimSavedPlaylist;

/**
 *
 * @author bill
 */
public class MusicObjects {

    private static List<SlimAlbum> albums;
    private static List<SlimAlbum> newMusic;
    private static List<SlimArtist> artists;
    private static HashMap<String, SlimAlbum> albumMap;
    private static HashMap<String, SlimArtist> artistMap;
    private static List<SlimSavedPlaylist> savedPlaylists;

    private static SlimComparator comparator = new SlimComparator();
    
    public static Collection<SlimAlbum> getAlbums() {
        return getAlbums(false);
    }

    public static Collection<SlimAlbum> getAlbums(boolean includeArtist) {
        if (albums == null) {
            refreshAlbums(includeArtist);
        }

        Collections.sort(albums, comparator);

        return albums;
    }

    public static Collection<SlimAlbum> getNewMusic() {
        return getNewMusic(false);
    }

    public static Collection<SlimAlbum> getNewMusic(boolean includeArtist) {
        if (newMusic == null) {
            refreshNewMusic(includeArtist);
        }

        return newMusic;
    }
    
    public static HashMap<String, SlimAlbum> getAlbumMap() {
        if (albumMap == null) {
            refreshAlbums();
        }
        return albumMap;
    }

    public static HashMap<String, SlimArtist> getArtistMap() {
        if (artistMap == null) {
            refreshArtists();
        }
        return artistMap;
    }

    public static Collection<SlimArtist> getArtists() {
        if (artists == null) {
            refreshArtists();
        }

        Collections.sort(artists, comparator);

        return artists;
    }

    public static Collection<SlimSavedPlaylist> getSavedPlaylists() {
        if (savedPlaylists == null) {
            refreshSavedPlaylists();
        }

        return savedPlaylists;
    }

    public static void refreshAlbums() {
        refreshAlbums(false);
    }

    public static void refreshAlbums(boolean includeArtist) {
        try {
            albums = new ArrayList<SlimAlbum>(JukeboxProperties.getInstance().getDatabase().getAlbums(includeArtist));

            Collections.sort(albums, comparator);

            albumMap = new HashMap<String, SlimAlbum>();
            for (SlimAlbum album : albums) {
                albumMap.put(album.getId(), album);
            }

        } catch (SlimConnectionException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void refreshNewMusic(boolean includeArtist) {
        try {
            newMusic = new ArrayList<SlimAlbum>(JukeboxProperties.getInstance().getDatabase().getNewMusic(includeArtist));
        } catch (SlimConnectionException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void refreshArtists() {
        try {
            artists = new ArrayList<SlimArtist>(JukeboxProperties.getInstance().getDatabase().getArtists());

            Collections.sort(artists, comparator);

            artistMap = new HashMap<String, SlimArtist>();
            for (SlimArtist artist : artists) {
                artistMap.put(artist.getId(), artist);
            }
        } catch (SlimConnectionException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void refreshSavedPlaylists() {
        try {
            savedPlaylists = new ArrayList<SlimSavedPlaylist>(JukeboxProperties.getInstance().getPlaylistManager().getPlaylists());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
